package com.compass.mobile.ui.dialog

import android.app.Dialog
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.compass.mobile.R
import com.compass.mobile.callback.OnCustomButtonClick
import com.compass.mobile.databinding.DialogInputPhoneBinding

import com.compass.mobile.framework.base.BaseDialog
import com.compass.mobile.framework.base.BaseDialogFragment
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.utils.FocusViewUtils
import countDownCoroutines
import getStringFromResource

/**
 * Description： 输入电话号码
 * Date:2024/6/19 16:48
 */
class InputPhoneDialog {
    class Builder(
        activity: FragmentActivity,
        onBottomClick: OnCustomButtonClick
    ) : BaseDialogFragment.Builder<Builder>(activity) {
        private val mBinding: DialogInputPhoneBinding =
            DialogInputPhoneBinding.inflate(LayoutInflater.from(activity))

        private var onBottomClick: OnCustomButtonClick
        private var phoneNumber: String? = null
        private var code: String? = null

        init {
            this.onBottomClick = onBottomClick
            setContentView(mBinding.root)
            setWidth((AppManager.getScreenHeightPx() * 0.6).toInt())
            setAnimStyle(BaseDialog.AnimStyle.DEFAULT)
            setGravity(Gravity.CENTER)
            FocusViewUtils.scaleView(mBinding.tvGetCode, 1)
            FocusViewUtils.scaleView(mBinding.tvConfirm, 1)
            initView()
        }

        private fun initView() {

            mBinding.tvGetCode.onClick {
                if (isClick) {
                    phoneNumber = mBinding.etPhone.text.toString().trim()
                    onBottomClick.onButtonCallBack(1, phoneNumber)
                    countDownCoroutines()
                }

            }

            mBinding.tvConfirm.onClick {
                code = mBinding.etPassword.text.toString().trim()
                onBottomClick.onButtonCallBack(2, code)
                dismiss()
            }

        }

        private var isClick = true

        private fun countDownCoroutines() {
            //倒计时
            countDownCoroutines(59, activity.lifecycleScope, onTick = {
                isClick = false
                mBinding.tvGetCode.text =
                    getStringFromResource(R.string.sms_tip, it.plus(1).toString())
            }) {
                isClick = true

                mBinding.tvGetCode.text = "获取验证码"
            }
        }

    }
}